<?php

use Drupal\shusy_product\Entity\ShusyProduct;
use Drupal\ssy_live_product\Entity\SsyLiveProduct;

function ssy_live_product_ssy_live_product_entity_form($form, &$form_state, SsyLiveProduct $e, ShusyProduct $product) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'ssy_live_product') . '/ssy_live_product.forms.inc';
  $form_state[SsyLiveProduct::class] = $e;

  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('商品名称'),
    '#required' => TRUE,
    '#default_value' => $e->name,
    '#element_validate' => ['ssy_live_product_validate_name_length'],
    '#description' => t('商品名称，最长14个汉字<br/> @name', ['@name' => $product->name]),
  ];
  $form['price_type'] = [
    '#required' => TRUE,
    '#default_value' => $e->price_type,
    '#type' => 'radios',
    '#title' => t('Poll status'),
    '#options' => [1 => '一口价', 2 => '价格区间', 3 => '显示折扣价'],
    '#description' => t('价格类型:<br/>1：一口价（只需要传入price，price2不传）<br/> 2：价格区间（price字段为左边界，price2字段为右边界，price和price2必传）<br/> 3：显示折扣价（price字段为原价，price2字段为现价， price和price2必传'),
  ];
  $form['price'] = [
    '#type' => 'textfield',
    '#title' => t('价格1'),
    '#required' => TRUE,
    '#default_value' => $e->price / 100,
    '#element_validate' => ['element_validate_number'],
    '#size' => 8,
  ];
  $form['price2'] = [
    '#type' => 'textfield',
    '#title' => t('价格2'),
    '#required' => TRUE,
    '#default_value' => $e->price2 / 100,
    '#element_validate' => ['element_validate_number'],
    '#size' => 8,
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  $form['actions']['preview'] = [
    '#type' => 'submit',
    '#value' => t('保存并审核'),
    '#submit' => ['ssy_live_product_ssy_live_product_entity_form_submit_audit'],
  ];
  return $form;
}

function ssy_live_product_validate_name_length($element, &$form_state) {
  $value = $element['#value'];
  if (mb_strlen($value) > 14) {
    form_error($element, t('%name 不能超过 14 个汉字.', ['%name' => $element['#title']]));
  }
}

function ssy_live_product_ssy_live_product_entity_form_submit($form, &$form_state) {
  $e = $form_state[SsyLiveProduct::class];
  if ($e instanceof SsyLiveProduct) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->price = bcmul($e->price, 100);
    $e->price2 = bcmul($e->price2, 100);
    $e->save();
  }
}

function ssy_live_product_ssy_live_product_entity_form_submit_audit($form, &$form_state) {
  $e = $form_state[SsyLiveProduct::class];
  if ($e instanceof SsyLiveProduct) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->price = bcmul($e->price, 100);
    $e->price2 = bcmul($e->price2, 100);
    $e->save();
    $ew = $e->wrapper();

    $image = $ew->field_image->value();
    //获取封面
    if (empty($image)) {
      $image = $ew->product->field_image->value();
      if (empty($image)) {
        $image = $ew->product->field_images->get(0)->value();
      }
    }
    if (!empty($image)) {
      //裁切 300*300
      $image300x300 = image_style_path('300x300', $image['uri']);
      image_style_create_derivative(image_style_load('300x300'), $image['uri'], $image300x300);
      //获取封面 裁切 300*300 并上传
      $app = wx_mini_program_get_app();
      $result = $app->media->uploadImage($image300x300);
      $media_id = $result['media_id'];
      $goods = [
        'coverImgUrl' => $media_id,
        'name' => $e->name,
        'priceType' => $e->price_type,
        'price' => bcdiv($e->price, 100, 2),
        'price2' => bcdiv($e->price2, 100, 2),
        'url' => $e->getMiniUrl(),
      ];
      $result = $app->broadcast->create($goods);
      if ($result['errcode'] == 0) {
        $e->goods_id = $result['goodsId'];
        $e->audit_id = $result['auditId'];
        $e->save();
        drupal_set_message('提交审核成功');
      }
    }

  }
}
